package com.hspedu.mhl.view;

import com.hspedu.mhl.dao.employDAO;
import com.hspedu.mhl.domain.DiningTable;
import com.hspedu.mhl.domain.Menu;
import com.hspedu.mhl.domain.employ;
import com.hspedu.mhl.service.BillService;
import com.hspedu.mhl.service.DiningTableService;
import com.hspedu.mhl.service.EmployService;
import com.hspedu.mhl.service.MenuService;
import com.hspedu.mhl.utils.Random_;
import com.hspedu.mhl.utils.Utility;

import java.util.List;


/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2023/04/05/15:38
 */

/**
 * loop 控制菜单
 * key 接收用户输入
 */
@SuppressWarnings("all")
public class MHLView {
    private boolean loop = true;
    private boolean loop2 = true;
    private String key = "";
    //定义一个EmployService对象
    private EmployService employService = new EmployService();
    private DiningTableService diningTableService = new DiningTableService();
    private MenuService menuService =new MenuService();
    //定义BillService属性
    private BillService billService=new BillService();

    //显示所有餐桌状态
    public void listDiningTable() {
        List<DiningTable> list = diningTableService.list();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable : list) {
            System.out.println(diningTable);
        }
        System.out.println("=================");
    }

    //完成订餐定座
    public void orderDiningTable() {
        System.out.println("==============预订餐桌================");
        System.out.println("请选择要预订的餐桌编号(-1表示退出 )");
        int orderId = Utility.readInt();
        if (orderId == -1) {
            System.out.println("=================取消预订餐桌=====================");
            return;
        }
        char key = Utility.readConfirmSelection();//这个返回值是Y或者N
        if (key == 'Y') { //
            //根据orderId返回对应的DiningTable，如果为null，说明对象不存在
            DiningTable diningTable = diningTableService.getDiningTableById(orderId);
            if (diningTable == null) {
                System.out.println("这个餐桌不存在");
                return;
            }
            if (!("空".equals(diningTable.getState()))) { //即不为空时，即有这个餐桌，判断有没有人定了，及判断状态
                System.out.println("有人预订或者在就餐中");
                return;
            }
            //终于可以定了  等等，先写信息
            System.out.print("输入预订人的名字:");
            String orderName = Utility.readString(50);
            System.out.print("输入预订人的电话:");
            String orderTel = Utility.readString(50);
            boolean b = diningTableService.orderDiningTable(orderId, orderName, orderTel);
            System.out.println(b == true ? "预订成功" : "预订失败");

        } else {
            System.out.println("============================取消订餐================");
        }


    }

    //完成点餐
    public void orderMenu(){
        System.out.println("================点餐服务==============");
        System.out.println("请输入点餐的桌号(-1退出)");
        int orderTableId = Utility.readInt();
        if (orderTableId==-1){
            System.out.println("取消点餐");
            return;
        }
        System.out.println("请输入菜品编号(-1退出)");
        int orderMenuId = Utility.readInt();
        if (orderMenuId==-1){
            System.out.println("取消点餐");
            return;
        }
        System.out.println("请输入点餐的数量(-1退出)");
        int orderNumbs = Utility.readInt();
        if (orderNumbs==-1){
            System.out.println("取消点餐");
            return;
        }
        //验证草桌号是否存在
        DiningTable diningTableById = diningTableService.getDiningTableById(orderTableId);
        if (diningTableById==null){
            System.out.println("餐桌号有误，不存在");
            return;
        }
        Menu menuById = menuService.getMenuById(orderMenuId);
        if (menuById==null){
            System.out.println("菜品号不存在");
            return;
        }
        //点餐
        boolean b = billService.orderMenu(orderMenuId, orderNumbs, orderTableId);
        if (b){
            System.out.println("==================点餐成功=================");
        }else {
            System.out.println("==================点餐失败==================");
        }
    }

    //完成结账
    public void payBill(){
        System.out.println("======================结账服务====================");
        System.out.println("请选择要结账的餐桌编号(-1)退出");
        int diningTable = Utility.readInt();
        if (diningTable==-1){
            System.out.println("取消结账============");
            return;
        }
        //验证餐桌是否存在
        DiningTable diningTableById = diningTableService.getDiningTableById(diningTable);
        if (diningTableById==null){
            System.out.println("结账的餐桌号不存在");
            return;
        }
        //验证餐桌是否需要结账
        boolean b = billService.hasPayBillByDiningTableID(diningTable);
        if (!b){
            System.out.println("该餐位没有未结账账单");
            return;
        }
        System.out.println("结账方式(现金/微信/支付宝)");
        //如果为null，则返回""  空串
        String payMode = Utility.readString(20, "");
        if("".equals(payMode)){
            System.out.println("取消结账");
            return;
        }

        //确认是否结账
        char key = Utility.readConfirmSelection();
        if (key=='Y'){


            //调用方法结账
            boolean b1 = billService.payBill(diningTable, payMode);
            if (b1){
                System.out.println(payMode+"结账成功");
            }else {
                System.out.println("结账失败");
            }
        }else {
            System.out.println("取消结账");
        }

    }

    /**
     * 往上是老师的，往下是我的
     */
    //显示主菜单的方法"
    public void mainMenu() {
        while (loop) {
            System.out.println("==================满汉楼==================");
            System.out.println("\t\t\t 1 登录满汉楼");
            System.out.println("\t\t\t 2 退出满汉楼");
            System.out.println("\t\t\t 3 注册满汉楼用户");
            System.out.print("请输入你的选择： ");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("请输入员工号： ");
                    String id = Utility.readString(32);
                    System.out.print("请输入密 码： ");
                    String pwd = Utility.readString(32);
                    employ emp = employService.getEmployByIdAndPwd(id, pwd);
                    if (emp != null) {//说明存在
                        System.out.println("=================登录成功(" + emp.getJob() + ")================");
                        SecondaryMenu();
                    } else {
                        System.out.println("登录失败");
                    }

                    /**
                     * boolean check = check(id, pwd);   !!自己写的  对了
                     *                     //数据库判断
                     *                     if (check) {
                     *                         System.out.println("身份验证成功，进入二级菜单");
                     *                         SecondaryMenu();
                     *                          * 回之后应该到达一级菜单，而不是直接执行break;好像也没必要，比如你退出qq，没必要再问你是不是还要登录
                     *                          * 就是不要使用同一个loop就行了
                     *
                     *                      } else{
                     *                          System.out.println("账号或密码错误");
                     *                              }
                     *
                     */
                    break;
                case "2":
                    System.out.println("退出满汉楼");
                    loop = false;
                    break;
                case "3":
                    Registered();
                    break;
                default:
                    System.out.println("闹甚了么");
            }
        }
    }

    /**
     * 二级菜单
     */
    public void SecondaryMenu() {
        while (loop2) {
            System.out.println("=================满汉楼二级菜单=================");
            System.out.println("\t\t\t 1 显示餐桌状态");
            System.out.println("\t\t\t 2 预订餐桌");
            System.out.println("\t\t\t 3 显示所有菜品");
            System.out.println("\t\t\t 4 点餐");
            System.out.println("\t\t\t 5 查看所有账单");
            System.out.println("\t\t\t 6 结账");
            System.out.println("\t\t\t 9 退出满汉楼 ");
            System.out.print("请输入选择：  ");
            String key = Utility.readString(1);
            switch (key) {
                case "1":
                    listDiningTable();
                    break;
                case "2":
                    orderDiningTable();
                    break;
                case "3":
                    menuService.list();
                    break;
                case "4":
                    orderMenu();
                    break;
                case "5":
//                    billService.list();
                    billService.list2();
                    break;
                case "6":
                    payBill();
                    break;
                case "9":
                    loop2 = false;
                    System.out.println("退出二级次菜单");
                    break;
                default:
                    System.out.println("看看你输的啥，卡bug的？");
            }
        }
    }

    /**
     * @param id  账号
     * @param pwd 密码
     * @return 在数据库判断账号密码是否正确
     */
    public boolean check(String id, String pwd) {
        employDAO employDAO = new employDAO();
        String sql = "select pwd from employ where empId=?";
        String PWD = (String) employDAO.queryScalar(sql, id);
        if (PWD != null && PWD.equals(pwd)) {//等于空的话也不用抛异常了，
            return true;
        }
        return false;
    }

    /**
     * @return 注册用户，返回判断是否注册成功
     */
    public boolean Registered() {
        boolean bol = true;  //后面循环的某一个逻辑变量
        String empId = null;
        System.out.println("那就注册一个吧：");
        System.out.print("请输入名字：");
        String name = Utility.readString(10);
        System.out.print("请输入电话号儿");
        String PhoneNumber = Utility.readString(12);
        System.out.print("请输入身份证号儿");
        String Identity_ID = Utility.readString(18);
        System.out.print("请输入现在的职业");
        String job = Utility.readString(20);
        System.out.print("请输入密码：");
        String pwd = Utility.readString(32);
        //判断这个随机数是否已存在，如果不存在则添加
        employDAO employDAO = new employDAO();
        String sql = "select empId from employ";
        List<employ> employs = employDAO.queryMulti(sql, employ.class);
        while (bol) { //维护了一下，不能有重复账号
            empId = Random_.RandomInt(1000000000, 1999999999) + "";
            //不知道该怎么验证使账号不能重复
            int j = 0;
            for (employ employ : employs) {
                if (empId.equals(employ.getEmpId())) {
                    j++;
                }
            }
            if (j == 0) {
                break;
            }
        }
        String sql1 = "insert into employ values(null,?,?,?,?,?,?)";
        System.out.print("数据库没此账号");
        int update = employDAO.update(sql1, empId, pwd, name, PhoneNumber, Identity_ID, job);
        if (update > 0) {
            System.out.println("添加成功");
            System.out.println("你的账号为" + empId);
            return true;
        }
        return false;
    }
}

/**
 * 按照设计模式的话需要把这些方法都放入到其他包和类中
 */